package com.sebiotec.login.viewmodel;

import android.app.Application;

import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;

import com.allen.library.interceptor.Transformer;
import com.allen.library.observer.CommonObserver;
import com.allen.library.observer.LoginCommonObserver;
import com.sebiotec.basecommon.utils.constant.SjmConstant;
import com.sebiotec.basecommon.utils.language.LocalManageUtil;
import com.sebiotec.basecommon.utils.tools.ActivityTool;
import com.sebiotec.login.model.get.ResponseLogin;
import com.sebiotec.login.model.get.UpdateApp;
import com.sebiotec.login.net.RadomApiHelper;
import com.sebiotec.login.ui.activity.LoadingDialog;

import java.util.HashMap;

public class LoginViewModel extends AndroidViewModel {
    public MyLoginDataCallBack mDataCallBack;

    //数据回调接口
    public interface MyLoginDataCallBack<T> {
        void onDataSuccessCallBack( T t);
        void onDataFailCallBack(T t,T t1);
    }
    public LoginViewModel(@NonNull Application application) {
        super(application);
    }
     //登录
    public void login(String userName, String pwd, final MyLoginDataCallBack mDataCallBack){
        LoadingDialog loadingDialog = new LoadingDialog(ActivityTool.getInstance().getLastActivity());
        HashMap<String,Object> map=new HashMap<String, Object>();
        map.put("LoginId",userName);
        map.put("password",pwd);
        map.put("Lang", LocalManageUtil.getSMJSetLanguageLocales(ActivityTool.getInstance().getLastActivity()));
        RadomApiHelper.geRadomApi()
                .Login(map,0)
                .compose(Transformer.<ResponseLogin>switchSchedulers(loadingDialog))
                .subscribe(new LoginCommonObserver<ResponseLogin>() {

                    @Override
                    protected String setTag() {
                        return SjmConstant.LOGINNET;
                    }

                    @Override
                    protected void onError(String errorMsg,String errorBody) {
                        mDataCallBack.onDataFailCallBack(errorMsg,errorBody);
                    }

                    @Override
                    protected void onSuccess(ResponseLogin s) {
                        mDataCallBack.onDataSuccessCallBack(s);
                    }
                });








    }





    public void Update( String name,final MyLoginDataCallBack mDataCallBack) {

        RadomApiHelper.geRadomApi()
                .UpdateApp(name)
                .compose(Transformer.<UpdateApp>switchSchedulers())
                .subscribe(new CommonObserver<UpdateApp>() {
                    @Override
                    protected String setTag() {
                        return SjmConstant.LOGINNET;
                    }
                    @Override
                    protected void onError(String errorMsg) {
                        mDataCallBack.onDataFailCallBack(errorMsg,"");
                    }

                    @Override
                    protected void onSuccess(UpdateApp s) {
                        mDataCallBack.onDataSuccessCallBack(s);
                    }
                });


    }


    }
